
package org.me.androidtimelineposition;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
import android.widget.Button;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends Activity {
	
    private HorizontalScrollView scroll1;
    private HorizontalScrollView scroll2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

		// Let's get the controls:
        scroll1 = (HorizontalScrollView)this.findViewById( R.id.scroll1 );
        scroll2 = (HorizontalScrollView)this.findViewById( R.id.scroll2 );

		// Disable the horizontal scrollbar:
        scroll1.setHorizontalScrollBarEnabled( false );
        scroll2.setHorizontalScrollBarEnabled( false );

		// Now attach the listeners so we know when to scroll:
        scroll1.setOnTouchListener( scrollOneListener );
        scroll2.setOnTouchListener( scrollTwoListener );

		//
		// The code below should be moved to a resize event handler since at this
		// moment the size of the scroll view is not known yet.
		//
		
		// The display log:
		TextView textView = (TextView)this.findViewById( R.id.log );
		textView.setText( "Event starts at 14:15 and the view is from 14:00 to 15:00" );

		// The view's start and end time:
		Date viewTimeBeg = stringToDate( "14:00" );
		Date viewTimeEnd = stringToDate( "15:00" );

		// Calculate the percentage of this event:
		Date eventTime = stringToDate( "14:15" );
		float percentage = timeToPercentage( viewTimeBeg, viewTimeEnd, eventTime);

		// Calculate the new position of the button:
		float position = 100 * percentage;

		// Find the first button and set the calculated position:
		Button button1 = (Button)this.findViewById( R.id.but1 );
		//button1.setX( position ); // This works in Android 3.0.

		int left = (int)position;
		int top = 0;
		int right = 50;
		int bottom = 10;
		button1.layout( left, top, right, bottom );
	}


    private OnTouchListener scrollOneListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            scroll2.scrollTo(scroll1.getScrollX(), 0);
            return false;
        }
    };

    private OnTouchListener scrollTwoListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            scroll1.scrollTo(scroll2.getScrollX(), 0);
            return false;
        }
    };

	private static Date stringToDate( String str )
	{
		Date result = null;

		// The formatter used to convert the strings to a time value:
        DateFormat dateFormat = new SimpleDateFormat("hh:mm");

        try
		{
            result = dateFormat.parse( str );
        }
		catch ( Exception e )
		{
        }

		return result;
	}

	private static float timeToPercentage( Date beg, Date end, Date x )
	{
		// The begin end end time in milliseconds:
		long begTime = beg.getTime();
		long endTime = end.getTime();

		// Let's calculate the difference between the times so that we know
		// what the area is to search in:
		long deltaTime = endTime - begTime;

		// Now we calculate the difference between the X-date and the begin
		// date. That way we can calulate the percentage.
		long xTime = x.getTime();
		long xDeltaTime = xTime - begTime;

		// Let's calculate the percentage:
		float percentage = xDeltaTime / ((Long)(deltaTime)).floatValue();

		return percentage;
	}
}